﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace EClock_Soft_Design
{
    /// <summary>
    /// Interaction logic for EClock.xaml
    /// </summary>
    public partial class EClock : UserControl
    {
        int _sec = 0, _min = 0 , _hour = 0;
        #region Property
        public int sec
        {
            get { return _sec; }
            set { _sec = value; }
        }
        public int min
        {
            get { return min; }
            set { min = value; }
        }
        public int hour
        {
            get { return hour; }
            set { hour = value; }
        }
        private int _numTick = 0;
        private DispatcherTimer clkTimer = new DispatcherTimer();
        #endregion

        #region Event
       
        
        #endregion
        

        #region Constructor
        public EClock()
        {
            InitializeComponent();
            clkTimer.Tick +=new EventHandler(clkTimer_Tick);
        }
        #endregion


        public void setImage()
        {
            this.img_sec1.Source = getImageSource(_sec / 10);
            this.img_sec2.Source = getImageSource(_sec - (_sec / 10) * 10);

            this.img_min1.Source = getImageSource(_min / 10);
            this.img_min2.Source = getImageSource(_min - (_min / 10) * 10);

            this.img_hour1.Source = getImageSource(_hour / 10);
            this.img_hour2.Source = getImageSource(_hour - (_hour / 10) * 10);
        }


        BitmapImage getImageSource(int number)
        {
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(@"Hinh/" + number + ".png", UriKind.Relative);
            img.EndInit();
            return img;
        }
        
        void clkTimer_Tick(object sender, EventArgs e)
        {
            _numTick++;
            if (_numTick == 1000)
            {
                _numTick = 0;
                _sec++;
                if (_sec >= 100)
                {
                    _min++;
                    _sec = _sec / 100;
                    if (_min >= 60)
                    {
                        _hour++;
                        _min = _min / 60;
                    }
                }
                setImage();
            }
        }
        public void Start()
        {
            clkTimer.Start();
        }

        public void Stop()
        {
            clkTimer.Stop();
            _min = 0;
            _sec = 0;
            _hour = 0;
            setImage();

        }
        public void Pause()
        {
            clkTimer.Stop();
        }
        
        public void Resume()
        {
            clkTimer.Start();
        }
    }
}
